home *** CD-ROM | disk | FTP | other *** search
/ Mission 3 / Mission 3.zip / Mission 3.iso / texte / qed / src / clipbrd.c < prev    next >
C/C++ Source or Header  |  1998-10-19  |  6KB  |  310 lines

  1. #include "global.h"
  2. #include "comm.h"
  3. #include "edit.h"
  4. #include "file.h"
  5. #include "icon.h"
  6. #include "memory.h"
  7. #include "options.h"
  8. #include "rsc.h"
  9. #include "text.h"
  10. #include "clipbrd.h"
  11.  
  12. /* exportierte Variablen ***************************************************/
  13. RING    clip_text;
  14. PATH    clip_dir;
  15.  
  16. /****** DEFINES ************************************************************/
  17.  
  18. #define MAX_UNDO    5
  19. #define END_UNDO    -1
  20.  
  21. /* loake Variablen *********************************************************/
  22. static bool    clip_dirty;
  23. static PATH    clip_name;
  24.  
  25. static int    undo[MAX_UNDO];
  26. static int    undo_anz;
  27. static RING    undo_text;
  28. static int    undo_ptr;
  29.  
  30. static char    save_col[MAX_LINE_LEN];        /* Gerettete Zeile für Undo */
  31. static int    save_len;
  32. static int    save_xpos;
  33.  
  34. static bool    last_was_bin = FALSE;
  35. static long    clip_timestamp = 0;
  36.  
  37. /*******************************************************************************/
  38.  
  39. void clr_undo(void)
  40. {
  41.     undo_ptr = -1;
  42.     undo_anz = 0;
  43. }
  44.  
  45. bool any_undo(void)
  46. {
  47.     if (undo_anz && undo[undo_anz-1]==END_UNDO)
  48.         return TRUE;
  49.     return FALSE;
  50. }
  51.  
  52. bool test_col_anders(void)
  53. {
  54.     int i;
  55.  
  56.     if (undo_anz == 0)
  57.         return FALSE;
  58.     if (undo[undo_anz-1] == COL_ANDERS)
  59.         return TRUE;
  60.     if (undo[undo_anz-1] != END_UNDO)
  61.         return FALSE;
  62.     for (i = 0; i < undo_anz; i++)
  63.         if (undo[i]==END_UNDO)
  64.             break;
  65.     if (i > 0 && i < undo_anz)
  66.         return (undo[i-1] == COL_ANDERS);
  67.     return FALSE;
  68. }
  69.  
  70. void end_undo_seq(void)
  71. {
  72.     int    i;
  73.  
  74.     undo_ptr = -1;
  75.     if (undo_anz==0 || undo[undo_anz-1]==END_UNDO)
  76.         return;
  77.     for (i=0; i<undo_anz; i++)
  78.         if (undo[i]==END_UNDO)
  79.         {
  80.             i++;
  81.             undo_anz -= i;
  82.             memcpy(undo, undo+i, (int) sizeof(int) * undo_anz);
  83.             break;
  84.         }
  85.     add_undo(END_UNDO);
  86. }
  87.  
  88. void add_undo(int undo_op)
  89. {
  90.     if (undo_anz<MAX_UNDO && (undo_anz==0 || undo[undo_anz-1]!=undo_op))
  91.         undo[undo_anz++] = undo_op;
  92. }
  93.  
  94. int get_undo(void)
  95. {
  96.     int i;
  97.  
  98.     if (undo_anz==0)
  99.         return NO_UNDO;
  100.     if (undo_ptr<0)
  101.     {
  102.         for (i=0; i<undo_anz; i++)
  103.             if (undo[i]==END_UNDO) break;
  104.         if (i==undo_anz) return NO_UNDO;
  105.         undo_ptr = i;
  106.     }
  107.     undo_ptr--;
  108.     if (undo_ptr<0)
  109.         return NO_UNDO;
  110.     return undo[undo_ptr];
  111. }
  112.  
  113. void undo_takes_text(RINGP r)
  114. {
  115.     kill_textring(&undo_text);
  116.     undo_text = *r;
  117.     FIRST(r)->vorg = &undo_text.head;
  118.     LAST(r)->nachf = &undo_text.tail;
  119. }
  120.  
  121. RINGP get_undo_text(void)
  122. {
  123.     return &undo_text;
  124. }
  125.  
  126. /*
  127.  * UNDO für eine Zeile
  128. */
  129. void get_undo_col(TEXTP t_ptr)
  130. {
  131.     if (!test_col_anders() || t_ptr->ypos!=undo_y)
  132.     {
  133.         undo_y = t_ptr->ypos;
  134.         save_len = t_ptr->cursor_line->len;
  135.         memcpy(save_col, TEXT(t_ptr->cursor_line), save_len);
  136.         save_xpos = t_ptr->xpos;
  137.     }
  138.     add_undo(COL_ANDERS);
  139. }
  140.  
  141. void do_undo_col(TEXTP t_ptr, int undo)
  142. {
  143.     ZEILEP    undo_col;
  144.     char        help[MAX_LINE_LEN];
  145.     char         *str;
  146.     int        length;
  147.  
  148.     if (undo == COL_ANDERS)
  149.     {
  150.         undo_col = get_line(&t_ptr->text,undo_y);
  151.         length = undo_col->len;
  152.         memcpy(help, TEXT(undo_col), length);
  153.  
  154.         str = REALLOC(&undo_col,0,save_len-length);
  155.         memcpy(str, save_col, save_len);
  156.  
  157.         memcpy(save_col, help, length);
  158.         save_len = length;
  159.  
  160.         t_ptr->moved++;
  161.         make_chg(t_ptr->link,LINE_CHANGE,undo_y);
  162.         make_chg(t_ptr->link,POS_CHANGE,0);             /* wg. moved */
  163.         t_ptr->xpos = save_xpos;
  164.         t_ptr->cursor_line = undo_col;
  165.         add_undo(COL_ANDERS);
  166.     }
  167. }
  168.  
  169.  
  170. /*******************************************************************************/
  171.  
  172. void save_clip(void)        /* nur wegschreiben */
  173. {
  174.     if (clip_on_disk && clip_dirty)
  175.     {
  176.         scrap_clear();
  177.         save_datei(clip_name, &clip_text, FALSE);
  178.         clip_timestamp = file_time(clip_name, NULL, NULL);
  179.         send_clip_change();
  180.         clip_dirty = FALSE;
  181.     }
  182. }
  183.  
  184. void load_clip(void)        /* nur laden */
  185. {
  186.     long    timestamp;
  187.     
  188.     /*
  189.      * Nur neu laden, wenn letzter Copy von qed bereits weggeschrieben wurde,
  190.      * ansonsten sind die aktuellen Klembrett-Daten noch in clip_text.
  191.     */
  192.     if (clip_on_disk && !clip_dirty)
  193.     {
  194.         timestamp = file_time(clip_name, NULL, NULL);
  195.         if (timestamp != clip_timestamp)
  196.         {
  197.             free_textring(&clip_text);
  198.             if (last_was_bin)
  199.                 clip_text.ending = binmode;
  200.             if (load_datei(clip_name, &clip_text, FALSE, NULL) != 0)
  201.                 free_textring(&clip_text);
  202.             clip_timestamp = timestamp;
  203.         }
  204.     }
  205. }
  206.  
  207.  
  208. void clip_takes_text(RINGP r)
  209. {
  210.     kill_textring(&clip_text);
  211.     clip_text = *r;
  212.     last_was_bin = (r->ending == binmode);
  213.     FIRST(r)->vorg = &clip_text.head;
  214.     LAST(r)->nachf = &clip_text.tail;
  215.     clip_dirty = TRUE;
  216. }
  217.  
  218. void clip_add_text(RINGP r)
  219. {
  220.     ZEILEP    col;
  221.  
  222.     col = LAST(&clip_text);            /* letzte Zeile */
  223.     col->nachf = FIRST(r);
  224.     FIRST(r)->vorg = col;
  225.     LAST(r)->nachf = &clip_text.tail;
  226.     LAST(&clip_text) = LAST(r);
  227.     clip_text.lines += r->lines;
  228.     col_concate(&col);
  229.     clip_text.lines--;
  230.     clip_dirty = TRUE;
  231. }
  232.  
  233. static int get_first_drive(void)
  234. {
  235.     unsigned long    drives;
  236.     int                drive;
  237.  
  238.     drives = Dsetdrv(Dgetdrv());                    /* Alle Laufwerke */
  239.     if (drives == 0)
  240.         drive = -1;
  241.     else if (drives <= 3)
  242.         drive = 0;                                        /* Benutze Laufwerk A */
  243.     else
  244.     {
  245.         drives >>= 2;
  246.         drive = 2;                                        /* Beginne bei Laufwerk C */
  247.         while (!(drives & 1) && drive < 32)    /* Laufwerk gefunden */
  248.         {
  249.             drive++;
  250.             drives >>= 1;
  251.         }
  252.     }
  253.     return drive;
  254. }
  255.  
  256. void init_clipbrd(void)
  257. {
  258.     PATH        s;
  259.     char        *str;
  260.  
  261.     scrp_read (clip_dir);                                    /* Scrap-Directory lesen */
  262.     if (clip_dir[0] == EOS)                                    /* Noch keines gesetzt */
  263.     {
  264.         if ((str=getenv("SCRAPDIR"))!=NULL && *str!=EOS)
  265.             strcpy(clip_dir, str);
  266.         else if ((str=getenv("CLIPBRD"))!=NULL && *str!=EOS)
  267.             strcpy(clip_dir, str);
  268.  
  269.         else
  270.         {
  271.             int drive;
  272.  
  273.             strcpy (clip_dir, "A:\\CLIPBRD\\");
  274.             drive = get_first_drive();
  275.             if (drive > 0)
  276.                 clip_dir[0] = 'A' + (char) drive;
  277.         }
  278.         scrp_write (clip_dir);                                /* Scrap-Directory setzen */
  279.     }
  280.     if (!make_normalpath(clip_dir))
  281.     {
  282.         if (clip_dir[0]=='A' || clip_dir[0]=='B' ||
  283.             clip_dir[0]=='a' || clip_dir[0]=='b')
  284.             clip_dir[0] = EOS;                        /* Kein Klemmbrett auf Disketten! */
  285.         else
  286.         {
  287.             strcpy (s, clip_dir);
  288.             s[strlen(s)-1] = EOS;                    /* Backslash löschen */
  289.             if (Dcreate(s) != 0)
  290.             {
  291.                 note(1, 0, NOSCRAP);
  292.                 clip_dir[0] = EOS;                    /* Kein Klemmbrett */
  293.             }
  294.         }
  295.     }
  296.     init_textring(&clip_text);
  297.     clip_dirty = FALSE;
  298.  
  299.     if (clip_dir[0] == EOS)
  300.         clip_on_disk = FALSE;
  301.     else
  302.     {
  303.         strcpy(clip_name, clip_dir);
  304.         strcat(clip_name, "scrap.txt");
  305.     }
  306.  
  307.     init_textring(&undo_text);
  308.     clr_undo();
  309. }
  310.